//-------------------------------------------------------------------------------------------------
// <copyright file="EditItem.cs" company="Microsoft">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// 
// <summary>
// Form to edit file properties for DUAScriptGen.
// </summary>
//-------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DUAScriptGen
{
	/// <summary>
	/// Summary description for EditItem.
	/// </summary>
	public class EditItem : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox Filename;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label2;

		public DUAFile dFile;

		private System.Windows.Forms.ComboBox comboAction;
		private System.Windows.Forms.RadioButton SpecialLocation;
		private System.Windows.Forms.RadioButton FixedLocation;
		private System.Windows.Forms.TextBox NewLocation;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.CheckBox checkCreateFolder;
		private System.Windows.Forms.TextBox txtExecuteCommandLine;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox txtDestFilename;
		private System.Windows.Forms.CheckBox checkReboot;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public EditItem()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			dFile = new DUAFile();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.Filename = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.checkCreateFolder = new System.Windows.Forms.CheckBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.FixedLocation = new System.Windows.Forms.RadioButton();
			this.SpecialLocation = new System.Windows.Forms.RadioButton();
			this.NewLocation = new System.Windows.Forms.TextBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtExecuteCommandLine = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.comboAction = new System.Windows.Forms.ComboBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.txtDestFilename = new System.Windows.Forms.TextBox();
			this.checkReboot = new System.Windows.Forms.CheckBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 10);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "Source file name";
			// 
			// Filename
			// 
			this.Filename.Location = new System.Drawing.Point(155, 8);
			this.Filename.Name = "Filename";
			this.Filename.ReadOnly = true;
			this.Filename.Size = new System.Drawing.Size(256, 20);
			this.Filename.TabIndex = 3;
			this.Filename.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.checkCreateFolder);
			this.groupBox1.Controls.Add(this.comboBox1);
			this.groupBox1.Controls.Add(this.FixedLocation);
			this.groupBox1.Controls.Add(this.SpecialLocation);
			this.groupBox1.Controls.Add(this.NewLocation);
			this.groupBox1.Location = new System.Drawing.Point(11, 200);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(400, 128);
			this.groupBox1.TabIndex = 7;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "File Location";
			// 
			// checkCreateFolder
			// 
			this.checkCreateFolder.Enabled = false;
			this.checkCreateFolder.Location = new System.Drawing.Point(120, 96);
			this.checkCreateFolder.Name = "checkCreateFolder";
			this.checkCreateFolder.Size = new System.Drawing.Size(120, 16);
			this.checkCreateFolder.TabIndex = 4;
			this.checkCreateFolder.Text = "Create folder first";
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Items.AddRange(new object[] {
														   "\\windows",
														   "\\windows\\system32",
														   "\\windows\\system32\\drivers",
														   "\\windows\\system32\\viewers",
														   "\\windows\\system32\\spool\\drivers\\color",
														   "\\windows\\inf",
														   "\\windows\\help",
														   "\\windows\\fonts",
														   "\\program files",
														   "\\documents and settings\\all users\\start menu",
														   "\\documents and settings\\all users\\start menu\\programs",
														   "\\documents and settings\\all users\\start menu\\programs\\startup",
														   "\\documents and settings\\all users\\desktop",
														   "\\documents and settings\\all users\\favorites"});
			this.comboBox1.Location = new System.Drawing.Point(120, 32);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(272, 21);
			this.comboBox1.TabIndex = 1;
			// 
			// FixedLocation
			// 
			this.FixedLocation.Checked = true;
			this.FixedLocation.Location = new System.Drawing.Point(8, 32);
			this.FixedLocation.Name = "FixedLocation";
			this.FixedLocation.TabIndex = 0;
			this.FixedLocation.TabStop = true;
			this.FixedLocation.Text = "Fixed Location";
			this.FixedLocation.CheckedChanged += new System.EventHandler(this.FixedLocation_CheckedChanged);
			// 
			// SpecialLocation
			// 
			this.SpecialLocation.Location = new System.Drawing.Point(8, 64);
			this.SpecialLocation.Name = "SpecialLocation";
			this.SpecialLocation.Size = new System.Drawing.Size(112, 24);
			this.SpecialLocation.TabIndex = 2;
			this.SpecialLocation.Text = "Special Location";
			this.SpecialLocation.CheckedChanged += new System.EventHandler(this.SpecialLocation_CheckedChanged);
			// 
			// NewLocation
			// 
			this.NewLocation.Location = new System.Drawing.Point(120, 64);
			this.NewLocation.Name = "NewLocation";
			this.NewLocation.Size = new System.Drawing.Size(272, 20);
			this.NewLocation.TabIndex = 3;
			this.NewLocation.Text = "";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.checkReboot);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Controls.Add(this.txtExecuteCommandLine);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Controls.Add(this.comboAction);
			this.groupBox2.Location = new System.Drawing.Point(11, 72);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(400, 120);
			this.groupBox2.TabIndex = 6;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Action";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 66);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Command line";
			// 
			// txtExecuteCommandLine
			// 
			this.txtExecuteCommandLine.Enabled = false;
			this.txtExecuteCommandLine.Location = new System.Drawing.Point(120, 64);
			this.txtExecuteCommandLine.Name = "txtExecuteCommandLine";
			this.txtExecuteCommandLine.Size = new System.Drawing.Size(272, 20);
			this.txtExecuteCommandLine.TabIndex = 3;
			this.txtExecuteCommandLine.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 34);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(88, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "Action to take";
			// 
			// comboAction
			// 
			this.comboAction.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboAction.Items.AddRange(new object[] {
															 "Move",
															 "Copy",
															 "Delete",
															 "Execute",
															 "Move and execute",
															 "Copy and execute"});
			this.comboAction.Location = new System.Drawing.Point(120, 32);
			this.comboAction.Name = "comboAction";
			this.comboAction.Size = new System.Drawing.Size(272, 21);
			this.comboAction.TabIndex = 1;
			this.comboAction.SelectedIndexChanged += new System.EventHandler(this.comboAction_SelectedIndexChanged);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(248, 344);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.OK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(336, 344);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 40);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(112, 16);
			this.label4.TabIndex = 4;
			this.label4.Text = "Destination file name";
			// 
			// txtDestFilename
			// 
			this.txtDestFilename.Location = new System.Drawing.Point(155, 40);
			this.txtDestFilename.Name = "txtDestFilename";
			this.txtDestFilename.ReadOnly = true;
			this.txtDestFilename.Size = new System.Drawing.Size(256, 20);
			this.txtDestFilename.TabIndex = 5;
			this.txtDestFilename.Text = "";
			// 
			// checkReboot
			// 
			this.checkReboot.Location = new System.Drawing.Point(120, 88);
			this.checkReboot.Name = "checkReboot";
			this.checkReboot.Size = new System.Drawing.Size(128, 24);
			this.checkReboot.TabIndex = 4;
			this.checkReboot.Text = "File could be in use";
			// 
			// EditItem
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(422, 379);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.Filename);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtDestFilename);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.Name = "EditItem";
			this.ShowInTaskbar = false;
			this.Text = "Edit File";
			this.Load += new System.EventHandler(this.EditItem_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void EditItem_Load(object sender, System.EventArgs e)
		{
			// Set default UI values based on DUAFile member variable
			Filename.Text = dFile.SourceFile;
			txtDestFilename.Text = dFile.DestFile;
			comboAction.SelectedIndex = dFile.Operation;
			txtExecuteCommandLine.Enabled = ( ((int)E_FILEOPERATION.Execute == dFile.Operation) || ((int)E_FILEOPERATION.CopyAndExecute == dFile.Operation) || ((int)E_FILEOPERATION.MoveAndExecute == dFile.Operation) );
			if (true == txtExecuteCommandLine.Enabled)
				txtExecuteCommandLine.Text = dFile.ExecuteCommandLine;

			if (false == dFile.bCustomLocation)
			{
				FixedLocation.Checked = true;
				SpecialLocation.Checked = false;
                comboBox1.Enabled = true;
				NewLocation.Enabled = false;
				checkCreateFolder.Enabled = false;
				comboBox1.SelectedIndex = comboBox1.FindString(dFile.DestLocation);
			}
			else
			{
				FixedLocation.Checked = false;
				SpecialLocation.Checked = true;
				comboBox1.Enabled = false;
				NewLocation.Enabled = true;
				checkCreateFolder.Enabled = true;
				NewLocation.Text = dFile.DestLocation;
				checkCreateFolder.Checked = dFile.bCreateCustomLocation;
				comboBox1.SelectedIndex = 0;
			}

			// The "file may be in use" checkbox only applies for copy and delete operations because the other file
			// operations can be accomplished even if the file is in use
			checkReboot.Enabled = ( ((int)E_FILEOPERATION.Copy == dFile.Operation) || ((int)E_FILEOPERATION.CopyAndExecute == dFile.Operation) || ((int)E_FILEOPERATION.Delete == dFile.Operation) );

			if (true == dFile.bRebootRequired)
				checkReboot.Checked = true;

		}

		private void Cancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult=DialogResult.Cancel;
			this.Close( );
		}


		/*****************************************************************************************
		Function Name:	OK_Click
		Author:			aaronste
		Description:	Update the DUAFile member variable with the user selections
		Inputs:			object sender - calling object
						System.EventArgs e - container for additional arguments for this event
		Outputs:		none
		******************************************************************************************/
		private void OK_Click(object sender, System.EventArgs e)
		{
			// Update the operation to perform for this file
			dFile.Operation = comboAction.SelectedIndex;

			// Update the execute command line as appropriate
			if ( ((int)E_FILEOPERATION.Execute == dFile.Operation) || ((int)E_FILEOPERATION.CopyAndExecute == dFile.Operation) || ((int)E_FILEOPERATION.MoveAndExecute == dFile.Operation) )
				dFile.ExecuteCommandLine = txtExecuteCommandLine.Text;
			else
				dFile.ExecuteCommandLine = "";

			// Update destination location info
			if (true == FixedLocation.Checked) 
			{
				dFile.DestLocation=comboBox1.Items[comboBox1.SelectedIndex].ToString();
				dFile.bCustomLocation = false;
				dFile.bCreateCustomLocation = false;
			} 
			else 
			{
				dFile.DestLocation=NewLocation.Text;
				dFile.bCustomLocation = true;
				dFile.bCreateCustomLocation = checkCreateFolder.Checked;
			}

			// Update the reboot setting if a file operation that reboots are applicable to was selected
			if (checkReboot.Enabled)
			{
				dFile.bRebootRequired = checkReboot.Checked;
			}
			else
			{
				dFile.bRebootRequired = false;
			}

			// Close the dialog box and send back an OK return code
			this.DialogResult=DialogResult.OK;
			this.Close( );
		}

		private void SpecialLocation_CheckedChanged(object sender, System.EventArgs e)
		{
			if (true == SpecialLocation.Checked)
			{
				comboBox1.Enabled = false;
				NewLocation.Enabled = true;
				checkCreateFolder.Enabled = true;
			}
			else
			{
				comboBox1.Enabled = true;
				NewLocation.Enabled = false;
				checkCreateFolder.Enabled = false;
			}
		}

		private void FixedLocation_CheckedChanged(object sender, System.EventArgs e)
		{
			if (true == FixedLocation.Checked)
			{
				comboBox1.Enabled = true;
				NewLocation.Enabled = false;
				checkCreateFolder.Enabled = false;
			}
			else
			{
				comboBox1.Enabled = false;
				NewLocation.Enabled = true;
				checkCreateFolder.Enabled = true;
			}
		}


		/*****************************************************************************************
		Function Name:	comboAction_SelectedIndexChanged
		Author:			aaronste
		Description:	Handler for the selected DUA action dropdown selection change event
		Inputs:			object sender - calling object
						System.EventArgs e - container for additional arguments for this event
		Outputs:		none
		******************************************************************************************/
		private void comboAction_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int iSelectedIndex = comboAction.SelectedIndex;

			// Disable or enable execute command line text box based on what action is selected
			txtExecuteCommandLine.Enabled = ( ((int)E_FILEOPERATION.Execute == iSelectedIndex) || ((int)E_FILEOPERATION.CopyAndExecute == iSelectedIndex) || ((int)E_FILEOPERATION.MoveAndExecute == iSelectedIndex) );

			// Disable or enable file in use check box based on what action is selected
			// The "file may be in use" checkbox only applies for copy and delete operations because the other file
			// operations can be accomplished even if the file is in use
			checkReboot.Enabled = ( ((int)E_FILEOPERATION.Copy == iSelectedIndex) || ((int)E_FILEOPERATION.CopyAndExecute == iSelectedIndex) || ((int)E_FILEOPERATION.Delete == iSelectedIndex) );
		}		
	}
}
